package core.util;

import gui.factory.components.AbstractDownload;

import java.io.File;

import javax.xml.xpath.XPathExpression;

import org.apache.commons.httpclient.methods.GetMethod;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import core.connector.ConnectionDetails;
import core.connector.DownloadableItem;
import core.connector.email.http.ConnectionDetailsHTTP;
import core.connector.email.http.HttpConnectorBase;
import core.xml.XPathResultNavigation;

public class AccountExtract {

	
	public static void main(String[] args) {
		P2mbrasilConnector conn = new P2mbrasilConnector(new ConnectionDetailsHTTP(1, "user", "pwd"));
		conn.connect();
	}
	
	
	private static class P2mbrasilConnector extends HttpConnectorBase{
		
		public P2mbrasilConnector(ConnectionDetails connectionDetails) {
			super(connectionDetails);
		}

		@Override
		public boolean connect() {
			GetMethod method = new GetMethod("");
			
			String resultado = null;
			try {
				client.executeMethod(method);
				resultado = getResponseBodyAsString(method);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			try {
	            TagNode root = getCleaner().clean(resultado);
	            Document dom = getDomSerializer().createDOM(root);
	            new PrettyXmlSerializer(getCleaner().getProperties()).writeToFile(root, "c:/P2m.xml");
				process(dom);
			} catch (Exception e) {
				e.printStackTrace();
			}			
			
			return true;
		}

		private void process(Document dom) throws Exception {
			XPathExpression mainPath = xpath.compile("//tr[contains(following-sibling::tr[1]/descendant::img/@src, 'barraopen') and child::td[@class='borderwrap']]");
			XPathExpression interPath = xpath.compile("//tr[td/@class='style6' and contains(descendant::img/@src, 'barraopen')]");			
			XPathExpression oldMainPath = xpath.compile("//tr[td[@class='style6' and (contains(text(),'OPEN') or contains(text(),'P2M'))]]");			
			
			XPathExpression titlePath = xpath.compile("descendant::div[@class='maintitle']/text()");
			XPathExpression oldTitlePath = xpath.compile("descendant::td[@class='style6']/text()");
			
			XPathExpression accountsPath = xpath.compile("descendant::table[1]//tr[position()>1]");
			
			XPathExpression accountTypePath = xpath.compile("td[1]/descendant::text()[last()]");
			XPathExpression accountLoginPath = xpath.compile("td[2]//input/@value");
			XPathExpression accountPwdPath = xpath.compile("td[3]//input/@value");
			
			XPathResultNavigation mainResult = new XPathResultNavigation(mainPath, dom);			
			if(mainResult.getElements().size() == 0){
				mainResult = new XPathResultNavigation(interPath, dom);
			}
			if(mainResult.getElements().size() == 0){
				mainResult = new XPathResultNavigation(oldMainPath, dom);
			}
			
			for(Element main : mainResult.getElements()){
				XPathExpression title = mainResult.getExpression() == mainPath ?  titlePath : oldTitlePath;
				XPathResultNavigation titleResult = new XPathResultNavigation(title , main);
				System.out.println(titleResult.getTextValue());
				
				Node sibling = title == titlePath ? main.getNextSibling().getNextSibling() : main.getNextSibling();
				Node contas = sibling;
				XPathResultNavigation contasResult = new XPathResultNavigation(accountsPath, contas);
				for(Element conta : contasResult.getElements()){
					XPathResultNavigation accountTypeResult = new XPathResultNavigation(accountTypePath, conta);
					XPathResultNavigation accountLoginResult = new XPathResultNavigation(accountLoginPath, conta);
					XPathResultNavigation accountPwdResult = new XPathResultNavigation(accountPwdPath, conta);
					System.out.println(String.format("	%-10s : %-30s %-100s",accountTypeResult.getTextValue(),accountLoginResult.getAttributeValue(),accountPwdResult.getAttributeValue()));					
				}
			}
			
		}
		
		
		@Override
		public void disconnect() {
		}

		@Override
		public int getNumberOfFiles() {
			return 0;
		}

		@Override
		public void getFile(DownloadableItem downloadableItem,File pastaDestino, AbstractDownload downloadGUI)throws Exception {			
		}

		@Override
		public boolean getNextFile() throws Exception {
			return false;
		}
		
		@Override
		protected String getCharset() {
			return "ISO-8859-1";
		}
		
	}
	
}
